package com.wframe.project.former.dataservices.marketing;

import com.frame.database.JBaseService;
import com.frame.database.JServiceFactory;
import com.frame.database.JdataBean;
import com.frame.database.PageResult;
import org.apache.commons.lang.StringUtils;
import org.json.JSONArray;

import java.util.List;

/**
 * Created by Administrator on 2017/6/3.
 */
public class SingService extends JBaseService<SingDao> {

    public static SingService getIntance() {
        return JServiceFactory.createJServiceProxy(SingService.class);
    }

    public PageResult querySinLe(JdataBean parms, int size, int pageno) throws Exception {
        return getDao().querySinLe(parms, size, pageno);
    }

    public JdataBean selectSinLeByid(int singleid) throws Exception {
        return getDao().selectSinLeByid(singleid);
    }

    public PageResult queryChooseProduct(JdataBean parms, int size, int pageno) throws Exception {
        return getDao().queryChooseProduct(parms, size, pageno);
    }

    public List<String> queryType(int productid) throws Exception {
        List<JdataBean> plist = getDao().queryType(productid);
        List<String> list = null;
        for (JdataBean pbean : plist) {
            list.add(pbean.getString("promotion_type"));
        }
        return list;
    }

    public void addControlsalesSingLe(String promotionid, JSONArray sales, String userids) throws Exception {
        getDao().deleteControlsalesAll(promotionid);
        String[] uids = null;
        if (StringUtils.isNotBlank(userids)) uids = userids.split(",");
        int iscontrol = 0;
        if (uids != null && uids.length > 0) iscontrol = 1;

        if (sales != null && sales.length() > 0){
            iscontrol = 1;
        }
        getDao().updatPromotecontrol(promotionid, iscontrol);//标记控销状态
        if (sales != null) {
            for (int i = 0; i < sales.length(); i++) {
                JdataBean bean = new JdataBean();
                bean.put("promotion_id", promotionid);
                bean.put("type", sales.getJSONObject(i).getString("type"));
                bean.put("val", sales.getJSONObject(i).getString("val"));
                bean.put("text", sales.getJSONObject(i).getString("text"));
                getDao().addControlsalesSingLe(bean);

            }
        }
        if (uids != null) {
            for (String uid : uids) {
                JdataBean bean = new JdataBean();
                bean.put("promotionid", promotionid);
                bean.put("type", 4);
                bean.put("val", uid);
                getDao().addControlsalesSingLe(bean);
            }

        }
    }

    public PageResult checkProduct(JdataBean parms, int size, int pageno) throws Exception {
        return getDao().checkProduct(parms, size, pageno);
    }

    public JdataBean queryproduct(int id) throws Exception {
        return getDao().queryproduct(id);
    }

    public JdataBean querySelectedProduct(int id) throws Exception {
        return getDao().querySelectedProduct(id);
    }
}
